package org.piggy.test.dubbo.web;

import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.piggy.test.dubbo.function.DubboInterface;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

@EnableDubbo
@SpringBootApplication
public class ConsumerApplication {

    private static final Logger logger = LoggerFactory.getLogger(ConsumerApplication.class);

    @DubboReference(version="1.0.0")
    private DubboInterface dubboInterface;

    public static void main(String[] args) {
        try {
            ConfigurableApplicationContext context = SpringApplication.run(ConsumerApplication.class, args);
            ConsumerApplication application = context.getBean(ConsumerApplication.class);
            String result = application.dubbo1();
            logger.info("sync result: {}", result);
            CompletableFuture<String> future = application.asyncDubbo1();
//            future.whenComplete((s, throwable) -> logger.info("async result: {}", s));
            logger.info("async result: {}", future.get());
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    }

    public String dubbo1() {
        return dubboInterface.dubbo1();
    }

    public CompletableFuture<String> asyncDubbo1() {
        return dubboInterface.asyncDubbo1();
    }
}
